package com.yuwenchao.superafflatus.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截器
 * 1 配置拦截器要拦截那些请求
 * 2 把拦截器放在容器中
 */
@Component
public class WebInterceptorFilter implements HandlerInterceptor {

    /**
     * 目标方法执行之前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     *
     */

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        //每次访问当前URI就会计数加1    这里建议将数据保存至数据库
        stringRedisTemplate.opsForValue().increment(requestURI);

        //request.getHeader("Proxy-Client-IP");

        return true;
    }

}
